home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / termsorc.lha / Extras / Source / term-source.lha / termRaster.c < prev    next >
C/C++ Source or Header  |  1995-09-26  |  9KB  |  523 lines

  1. /*
  2. **    termRaster.c
  3. **
  4. **    Screen character (raster) buffering routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* DeleteRaster():
  13.      *
  14.      *    Free the contents of the character raster.
  15.      */
  16.  
  17. VOID
  18. DeleteRaster()
  19. {
  20.     if(RasterSemaphore)
  21.     {
  22.         FreeVecPooled(RasterSemaphore);
  23.  
  24.         RasterSemaphore = NULL;
  25.     }
  26.  
  27.     if(Raster)
  28.     {
  29.         FreeVecPooled(Raster);
  30.  
  31.         Raster = NULL;
  32.     }
  33.  
  34.     if(RasterAttr)
  35.     {
  36.         FreeVecPooled(RasterAttr);
  37.  
  38.         RasterAttr = NULL;
  39.     }
  40. }
  41.  
  42.     /* CreateRaster():
  43.      *
  44.      *    Create the character raster.
  45.      */
  46.  
  47. BYTE
  48. CreateRaster()
  49. {
  50.         /* Width of the screen * 2 (in characters),
  51.          * extra for double width. The window size
  52.          * may change, the screen size hopefully
  53.          * doesn't.
  54.          */
  55.  
  56.     RasterWidth    = (Window -> WScreen -> Width / TextFontWidth) * 2;
  57.  
  58.         /* Height of the character raster. */
  59.  
  60.     RasterHeight    = Window -> WScreen -> Height / TextFontHeight;
  61.  
  62.         /* Allocate the raster. */
  63.  
  64.     if(Raster = (STRPTR)AllocVecPooled(RasterWidth * RasterHeight,MEMF_ANY | MEMF_CLEAR))
  65.     {
  66.             /* Allocate the raster attributes. */
  67.  
  68.         if(RasterAttr = (STRPTR)AllocVecPooled(RasterHeight,MEMF_ANY | MEMF_CLEAR))
  69.         {
  70.             if(RasterSemaphore = (struct SignalSemaphore *)AllocVecPooled(sizeof(struct SignalSemaphore),MEMF_ANY))
  71.             {
  72.                 InitSemaphore(RasterSemaphore);
  73.  
  74.                 return(TRUE);
  75.             }
  76.         }
  77.     }
  78.  
  79.     DeleteRaster();
  80.  
  81.     return(FALSE);
  82. }
  83.  
  84.     /* RasterEraseScreen(BYTE Mode):
  85.      *
  86.      *    Erase parts of the screen.
  87.      */
  88.  
  89. VOID __regargs
  90. RasterEraseScreen(BYTE Mode)
  91. {
  92.     LONG    First,
  93.         Last;
  94.  
  95.     ObtainSemaphore(RasterSemaphore);
  96.  
  97.     switch(Mode)
  98.     {
  99.         case 1:    First    = 0;
  100.             Last    = CursorY * RasterWidth + CursorX + 1;
  101.  
  102. //            SaveRaster(0,CursorY);
  103.  
  104.             memset(RasterAttr,SCALE_NORMAL,CursorY + 1);
  105.  
  106.             break;
  107.  
  108.         case 2:    First    = 0;
  109.             Last    = RasterHeight * RasterWidth - 1;
  110.  
  111. //            SaveRaster(0,RasterHeight - 1);
  112.  
  113.             memset(RasterAttr,SCALE_NORMAL,RasterHeight);
  114.  
  115.             break;
  116.  
  117.         default:First    = CursorY * RasterWidth + CursorX;
  118.             Last    = RasterHeight * RasterWidth - 1;
  119.  
  120. //            SaveRaster(CursorY,RasterHeight - 1);
  121.  
  122.             memset(&RasterAttr[CursorY],SCALE_NORMAL,RasterHeight - CursorY);
  123.  
  124.             break;
  125.     }
  126.  
  127.     RethinkRasterLimit();
  128.  
  129.     if(Last > First)
  130.         memset(&Raster[First],' ',Last - First);
  131.  
  132.     ConFontScaleUpdate();
  133.  
  134.     ReleaseSemaphore(RasterSemaphore);
  135. }
  136.  
  137.     /* RasterEraseLine(BYTE Mode):
  138.      *
  139.      *    Erase parts of the current cursor line.
  140.      */
  141.  
  142. VOID __regargs
  143. RasterEraseLine(BYTE Mode)
  144. {
  145.     LONG    First,
  146.         Last;
  147.  
  148.     ObtainSemaphore(RasterSemaphore);
  149.  
  150. //    SaveRaster(CursorY,CursorY);
  151.  
  152.     switch(Mode)
  153.     {
  154.             /* From beginning to current cursor position. */
  155.  
  156.         case 1:    First    = CursorY * RasterWidth;
  157.             Last    = First + CursorX + 1;
  158.  
  159.             break;
  160.  
  161.             /* Entire line. */
  162.  
  163.         case 2:    First    = CursorY * RasterWidth;
  164.             Last    = First + RasterWidth - 1;
  165.  
  166.             break;
  167.  
  168.             /* From current cursor position towards end. */
  169.  
  170.         default:First    = CursorY * RasterWidth + CursorX;
  171.             Last    = (CursorY + 1) * RasterWidth - 1;
  172.  
  173.             break;
  174.     }
  175.  
  176.     if(Last > First)
  177.         memset(&Raster[First],' ',Last - First);
  178.  
  179.     ReleaseSemaphore(RasterSemaphore);
  180. }
  181.  
  182.     /* RasterEraseCharacters(WORD Chars):
  183.      *
  184.      *    Erase a number of characters in the current cursor
  185.      *    line.
  186.      */
  187.  
  188. VOID __regargs
  189. RasterEraseCharacters(WORD Chars)
  190. {
  191.     if(CursorX < RasterWidth - 1)
  192.     {
  193.         LONG     First,
  194.              Diff;
  195.         UBYTE    *To,
  196.             *From;
  197.  
  198. //        SaveRaster(CursorY,CursorY);
  199.  
  200.         ObtainSemaphore(RasterSemaphore);
  201.  
  202.         First    = CursorY * RasterWidth + CursorX;
  203.         To    = &Raster[First];
  204.  
  205.         if(CursorX + Chars >= RasterWidth)
  206.         {
  207.             Diff = RasterWidth - 1 - CursorX;
  208.  
  209.             while(Diff-- > 0)
  210.                 *To++ = ' ';
  211.         }
  212.         else
  213.         {
  214.             From    = &Raster[First + Chars];
  215.             Diff    = RasterWidth - (CursorX + 1 + Chars);
  216.  
  217.             while(Diff--)
  218.             {
  219.                 *To++ = *From;
  220.  
  221.                 *From++ = ' ';
  222.             }
  223.         }
  224.  
  225.         ReleaseSemaphore(RasterSemaphore);
  226.     }
  227. }
  228.  
  229.     /* RasterClearLine(WORD Lines):
  230.      *
  231.      *    Clear and remove a number of lines.
  232.      */
  233.  
  234. VOID __regargs
  235. RasterClearLine(WORD Lines,WORD Top)
  236. {
  237.     if(Lines)
  238.     {
  239.         LONG RegionBottom;
  240.  
  241.         ObtainSemaphore(RasterSemaphore);
  242.  
  243.         if(RegionSet)
  244.             RegionBottom = Bottom;
  245.         else
  246.             RegionBottom = LastLine + 1;
  247.  
  248.         if(Top + Lines >= RegionBottom + 1)
  249.         {
  250.             SaveRaster(Top,RegionBottom);
  251.  
  252.             Lines = RegionBottom - Top + 1;
  253.  
  254.             memset(&Raster[Top * RasterWidth],' ',RasterWidth * Lines);
  255.             memset(&RasterAttr[Top * RasterWidth],SCALE_NORMAL,RasterWidth * Lines);
  256.         }
  257.         else
  258.         {
  259.             LONG     Max;
  260.             UBYTE    *From,
  261.                 *To;
  262.  
  263.             SaveRaster(Top,Top + Lines - 1);
  264.  
  265.             Max    = (RegionBottom - (Top + Lines)) * RasterWidth;
  266.  
  267.             From    = &Raster[(Top + Lines) * RasterWidth];
  268.             To    = &Raster[ Top          * RasterWidth];
  269.  
  270.             while(Max--)
  271.             {
  272.                 *To++ = *From;
  273.  
  274.                 *From++ = ' ';
  275.             }
  276.  
  277.             memset(&RasterAttr[RegionBottom - Lines],SCALE_NORMAL,Lines);
  278.         }
  279.  
  280.         RethinkRasterLimit();
  281.  
  282.         ConFontScaleUpdate();
  283.  
  284.         ReleaseSemaphore(RasterSemaphore);
  285.     }
  286. }
  287.  
  288.     /* RasterInsertLine(WORD Lines):
  289.      *
  290.      *    Insert a number of lines at the current cursor line.
  291.      */
  292.  
  293. VOID __regargs
  294. RasterInsertLine(WORD Lines,WORD Top)
  295. {
  296.     if(Lines)
  297.     {
  298.         LONG RegionBottom;
  299.  
  300.         if(RegionSet)
  301.             RegionBottom = Bottom;
  302.         else
  303.             RegionBottom = LastLine + 1;
  304.  
  305.         if(Top + Lines >= RegionBottom + 1)
  306.         {
  307.             SaveRaster(Top,RegionBottom);
  308.  
  309.             Lines = RegionBottom - Top + 1;
  310.  
  311.             memset(&Raster[Top * RasterWidth],' ',RasterWidth * Lines);
  312.             memset(&RasterAttr[Top * RasterWidth],SCALE_NORMAL,RasterWidth * Lines);
  313.         }
  314.         else
  315.         {
  316.             LONG     From,To,
  317.                  Max;
  318.             UBYTE    *FromPtr,
  319.                 *ToPtr;
  320.  
  321.             SaveRaster(RegionBottom - Lines,RegionBottom);
  322.  
  323.             ObtainSemaphore(RasterSemaphore);
  324.  
  325.             Max    = (RegionBottom - Lines - Top) * RasterWidth;
  326.  
  327.             From    = (RegionBottom - Lines) * RasterWidth - 1;
  328.             To    =  RegionBottom          * RasterWidth - 1;
  329.  
  330.             FromPtr    = &Raster[From];
  331.             ToPtr    = &Raster[To];
  332.  
  333.             while(Max--)
  334.                 *ToPtr-- = *FromPtr--;
  335.  
  336.             memset(&Raster[Top * RasterWidth],' ',Lines * RasterWidth);
  337.  
  338.             ReleaseSemaphore(RasterSemaphore);
  339.         }
  340.     }
  341. }
  342.  
  343.     /* RasterScrollRegion(WORD Direction,WORD RasterTop,WORD RasterBottom,WORD RasterLines):
  344.      *
  345.      *    Scroll the contents of the character raster up/down.
  346.      */
  347.  
  348. VOID __regargs
  349. RasterScrollRegion(WORD Direction,WORD RasterTop,WORD RasterBottom,WORD RasterLines)
  350. {
  351.     WORD Dir = ABS(Direction);
  352.  
  353.     ObtainSemaphore(RasterSemaphore);
  354.  
  355.     SaveRaster(RasterTop,RasterTop + RasterLines - 1);
  356.  
  357.     if(Dir >= RasterLines)
  358.     {
  359.             /* All that is needed is to delete the lines. */
  360.  
  361.         memset(&Raster[RasterTop * RasterWidth],' ',RasterLines * RasterWidth);
  362.     }
  363.     else
  364.     {
  365.         LONG     First,
  366.              Last,
  367.              Max,
  368.              i;
  369.         UBYTE    *From,
  370.             *To;
  371.  
  372.         Max = (RasterLines - Dir) * RasterWidth;
  373.  
  374.         if(Direction < 0)
  375.         {
  376.             First    = (RasterTop + RasterLines - Dir) * RasterWidth - 1;
  377.             Last    = (RasterTop + RasterLines    ) * RasterWidth - 1;
  378.  
  379.             From    = &Raster[First];
  380.             To    = &Raster[Last];
  381.  
  382.             while(Max--)
  383.                 *To-- = *From--;
  384.  
  385.             for(i = RasterBottom ; i >= (RasterTop + Dir) ; i--)
  386.                 RasterAttr[i] = RasterAttr[i - Dir];
  387.  
  388.             memset(&Raster[RasterTop * RasterWidth],' ',RasterWidth * Dir);
  389.  
  390.             memset(&RasterAttr[RasterTop],SCALE_NORMAL,Dir);
  391.         }
  392.         else
  393.         {
  394.             First    = RasterTop * RasterWidth + RasterWidth * Dir;
  395.             Last    = RasterTop * RasterWidth;
  396.  
  397.             From    = &Raster[First];
  398.             To    = &Raster[Last];
  399.  
  400.             while(Max--)
  401.                 *To++ = *From++;
  402.  
  403.             memset(&Raster[(RasterBottom - Dir) * RasterWidth],' ',RasterWidth * Dir);
  404.  
  405.             for(i = RasterTop ; i <= (RasterBottom - Dir) ; i++)
  406.                 RasterAttr[i] = RasterAttr[i + Dir];
  407.  
  408.             memset(&RasterAttr[RasterBottom - Dir],SCALE_NORMAL,Dir);
  409.         }
  410.     }
  411.  
  412.     RethinkRasterLimit();
  413.  
  414.     ConFontScaleUpdate();
  415.  
  416.     ReleaseSemaphore(RasterSemaphore);
  417. }
  418.  
  419.     /* RasterShiftChar(WORD Size):
  420.      *
  421.      *    Shift the characters following the current cursor
  422.      *    position Size characters to the right.
  423.      */
  424.  
  425. VOID __regargs
  426. RasterShiftChar(WORD Size)
  427. {
  428.     LONG     i,
  429.          First;
  430.     UBYTE    *From,
  431.         *To;
  432.  
  433.     ObtainSemaphore(RasterSemaphore);
  434.  
  435.     if(CursorX + Size >= RasterWidth - 1)
  436.     {
  437.         i    = RasterWidth - 1 - CursorX;
  438.         To    = &Raster[CursorY * RasterWidth + CursorX];
  439.  
  440.         while(i-- > 0)
  441.             *To++ = ' ';
  442.     }
  443.     else
  444.     {
  445.         First    = (CursorY + 1) * RasterWidth - 1;
  446.         To    = &Raster[First];
  447.  
  448.         From    = &Raster[First - Size];
  449.         i    = RasterWidth - Size;
  450.  
  451.         while(i-- > CursorX)
  452.             *To-- = *From--;
  453.  
  454.         To    = &Raster[CursorY * RasterWidth + CursorX];
  455.  
  456.         while(Size--)
  457.             *To++ = ' ';
  458.     }
  459.  
  460.     ReleaseSemaphore(RasterSemaphore);
  461. }
  462.  
  463.     /* RasterPutString(STRPTR String,WORD Length):
  464.      *
  465.      *    Put a string into the character raster.
  466.      */
  467.  
  468. VOID __regargs
  469. RasterPutString(STRPTR String,WORD Length)
  470. {
  471.     ObtainSemaphore(RasterSemaphore);
  472.  
  473.     if(Length == 1)
  474.     {
  475.         if(CursorX + 1 < RasterWidth)
  476.             Raster[CursorY * RasterWidth + CursorX] = String[0];
  477.     }
  478.     else
  479.     {
  480.         if(CursorX + Length >= RasterWidth)
  481.             Length = RasterWidth - 1 - CursorX;
  482.  
  483.         if(Length > 0)
  484.             memcpy(&Raster[CursorY * RasterWidth + CursorX],String,Length);
  485.     }
  486.  
  487.     ReleaseSemaphore(RasterSemaphore);
  488. }
  489.  
  490. VOID __regargs
  491. SaveRasterDummy(WORD First,WORD Last)
  492. {
  493. }
  494.  
  495. VOID __regargs
  496. SaveRasterReal(WORD First,WORD Last)
  497. {
  498.     STRPTR    Line,This;
  499.     LONG    Size;
  500.     WORD    i;
  501.  
  502.     ObtainSemaphore(RasterSemaphore);
  503.  
  504.     if(First < 0)
  505.         First = 0;
  506.  
  507.     if(Last > RasterHeight - 1)
  508.         Last = RasterHeight - 1;
  509.  
  510.     for(i = First, This = &Raster[(LONG)First * RasterWidth] ; i <= Last ; i++, This += RasterWidth)
  511.     {
  512.         Line = This;
  513.         Size = RasterWidth - 1;
  514.  
  515.         while(Size > 0 && Line[Size - 1] == ' ')
  516.             Size--;
  517.  
  518.         AddLine(Line,Size);
  519.     }
  520.  
  521.     ReleaseSemaphore(RasterSemaphore);
  522. }
  523.